package cn.hl.hm.controller.manage;

import cn.hl.hm.entity.RechargeRecoard;
import cn.hl.hm.logic.manage.RechargeRecoardWithdrawalLogic;
import cn.hl.hm.util.ExportData;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;
import cn.hl.hm.vo.RechargeRecoardWithdrawalVo;
import com.chinapay.util.date.DateUtil;
import org.apache.tools.ant.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 配载费提现审核功能
 * Created by xianghan on 17/12/26.
 */
@Controller
@RequestMapping("/manage/rechargeRecoard/withdrawal")
public class RechargeRecoardWithdrawalController {
    private static final Logger logger = LoggerFactory.getLogger(RechargeRecoardWithdrawalController.class);
    @Autowired
    private RechargeRecoardWithdrawalLogic withdrawalLogic;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestParam String driverPhone,
                      @RequestParam String bankName, @RequestParam String bankCard,
                      @RequestParam String cardholder, @RequestParam Double withdrawalCash,
                      @RequestParam String withdrawalReason, @RequestParam String reportPerson,
                      @RequestParam Integer reportUserId,
                      HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("运营管理---新增提现---rechargeRecoard/withdrawal/add");
            request.setCharacterEncoding("UTF-8");
            Boolean b=withdrawalLogic.add(driverPhone, bankName, bankCard, cardholder, withdrawalCash, withdrawalReason,
                    reportPerson, reportUserId);
            if (null==b) {
                Result.ok(null, InfoCode.DRIVER_WITHDRAWCASH_ERROR, response);
            } else if (b){
                Result.ok(null, InfoCode.SUCCESS, response);
            }else{
                Result.ok(null, InfoCode.FAILURE, response);
            }
        } catch (Exception e) {
            Result.ok(null, InfoCode.FAILURE, response);
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/operation/check", method = RequestMethod.POST)
    public String operationCheck(@RequestParam String id,
                                 @RequestParam String status, @RequestParam String auditReason,
                                 @RequestParam String assessorUsername, @RequestParam String assessorUserId,
                                 HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("运营管理---运营审核---rechargeRecoard/withdrawal/operation/check");
            request.setCharacterEncoding("UTF-8");
            if (withdrawalLogic.operationCheck(Integer.valueOf(id), status, auditReason, assessorUsername, Integer.valueOf(assessorUserId))) {
                Result.ok(null, InfoCode.SUCCESS, response);
            } else {
                Result.ok(null, InfoCode.FAILURE, response);
            }
        } catch (Exception e) {
            Result.ok(null, InfoCode.FAILURE, response);
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/financial/check", method = RequestMethod.POST)
    public String financialCheck(@RequestParam String id,
                                 @RequestParam String status, @RequestParam String auditReason,
                                 @RequestParam String assessorUsername, @RequestParam String assessorUserId,
                                 HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("运营管理---财务审核---rechargeRecoard/withdrawal/financial/check");
            request.setCharacterEncoding("UTF-8");
            if (withdrawalLogic.financialCheck(Integer.valueOf(id), status, auditReason, assessorUsername, Integer.valueOf(assessorUserId))) {
                Result.ok(null, InfoCode.SUCCESS, response);
            } else {
                Result.ok(null, InfoCode.FAILURE, response);
            }
        } catch (Exception e) {
            Result.ok(null, InfoCode.FAILURE, response);
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/financial/confirmPayment", method = RequestMethod.POST)
    public String confirmPayment(@RequestParam String id,
                                 @RequestParam String userId,
                                 HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("运营管理---财务确认支付---rechargeRecoard/withdrawal/financial/confirmPayment");
            request.setCharacterEncoding("UTF-8");
            if (withdrawalLogic.confirmPayment(Integer.valueOf(id), Integer.valueOf(userId))) {
                Result.ok(null, InfoCode.SUCCESS, response);
            } else {
                Result.ok(null, InfoCode.FAILURE, response);
            }
        } catch (Exception e) {
            Result.ok(null, InfoCode.FAILURE, response);
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/operationAssessor/list", method = RequestMethod.GET)
    public String operationAssessors(HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("运营管理---运营审核员列表---rechargeRecoard/withdrawal/operationAssessor/list");
            request.setCharacterEncoding("UTF-8");
            Result.ok(withdrawalLogic.operationAssessors(), InfoCode.SUCCESS, response);
        } catch (Exception e) {
            Result.ok(null, InfoCode.FAILURE, response);
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/financialAssessor/list", method = RequestMethod.GET)
    public String financialAssessors(HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("运营管理---财务审核员列表---rechargeRecoard/withdrawal/financialAssessor/list");
            request.setCharacterEncoding("UTF-8");
            Result.ok(withdrawalLogic.financialAssessors(), InfoCode.SUCCESS, response);
        } catch (Exception e) {
            Result.ok(null, InfoCode.FAILURE, response);
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(@RequestParam(required = false) String createTime,
                       @RequestParam String status, @RequestParam(required = false) String driverPhone,
                       @RequestParam(required = false) String driverName, @RequestParam(required = false) String operationAssessorName,
                       @RequestParam(required = false) String financialAssessorName,
                       @RequestParam(required = false, defaultValue = "1") Integer pageId,
                       @RequestParam(required = false, defaultValue = "10") Integer pageCount,
                       ModelMap modelMap,
                       HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("运营管理---提现列表---rechargeRecoard/withdrawal/list");
            request.setCharacterEncoding("UTF-8");
            withdrawalLogic.selectList(createTime, status, driverPhone, driverName, operationAssessorName, financialAssessorName,pageId,pageCount,modelMap);
            modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.addAttribute("infoCode", InfoCode.FAILURE);
        }
        return "manage/rechargeRecoard/withdrawal.list";
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(@RequestParam(required = false) String createTime,
                       @RequestParam String status, @RequestParam(required = false) String driverPhone,
                       @RequestParam(required = false) String driverName, @RequestParam(required = false) String operationAssessorName,
                       @RequestParam(required = false) String financialAssessorName,
                       @RequestParam(required = false, defaultValue = "1") Integer pageId,
                       @RequestParam(required = false, defaultValue = "1000000") Integer pageCount,
                       ModelMap modelMap,
                       HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("运营管理---提现列表导出---rechargeRecoard/withdrawal/export");
            String ctxPath = request.getSession().getServletContext().getRealPath("/") + "export/";
            String fileName = "";
            File file = new File(ctxPath.toString());
            if (!file.exists()) {
                file.mkdir();
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            withdrawalLogic.selectList(createTime, status, driverPhone, driverName, operationAssessorName, financialAssessorName,pageId,pageCount,modelMap);
            List<RechargeRecoardWithdrawalVo> rechargeRecoardWithdrawalVos =(List<RechargeRecoardWithdrawalVo>) modelMap.get("rechargeRecoardWithdrawalList");
            fileName= System.currentTimeMillis()+".xls";
            ExportData.exportRRWListExcel(ctxPath + fileName, rechargeRecoardWithdrawalVos);

            FileInputStream input=new FileInputStream(ctxPath + fileName);
            OutputStream output=response.getOutputStream();
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
            byte[] b=new byte[1024*1024];
            int len=0;
            while((len=input.read(b))!=-1){
                output.write(b,0,len);
            }
            input.close();
            output.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ;
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String list(@RequestParam Integer rrwId,
                       ModelMap modelMap,
                       HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("运营管理---提现详情---rechargeRecoard/withdrawal/detail");
            request.setCharacterEncoding("UTF-8");
            modelMap.put("rrw",withdrawalLogic.selectDetail(rrwId));
            modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.addAttribute("infoCode", InfoCode.FAILURE);
        }
        return "manage/rechargeRecoard/withdrawal.detail";
    }

}
